//
//  WYVisitorView.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/9.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SnapKit
class WYVisitorView: UIView {
    
    //点击 闭包 执行登陆或者注册
    var loginClosure: (() -> ())?
    
    //旋转试图 
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //设置罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息内容
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        
        //多行显示
        label.numberOfLines = 0
        //显示居中
        label.textAlignment = .Center
        
        
        return label
    }()
    

    //注册按钮
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return button
       
    }()
    
    //登陆按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitle("登陆", forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        backgroundColor = UIColor(white: 237/255, alpha: 1.0)
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //自动布局
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }

         iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top .equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
    }
    
    //开启旋转动画
    private func startAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    func updateVisitorinfo(message: String?, imageName: String?) {
    
        if let msg = message, imgName = imageName {
        
            messageLabel.text = msg
            cycleImageView.image = UIImage(named: imgName)
            //隐藏旋转视图
            cycleImageView.hidden = true
        } else {
        
            cycleImageView.hidden = false
            
            startAnimation()
        }
        
        
        
    }
    
    //点击事件 注册 
   @objc private func registerAction() {
    
    print("点击注册")
    
    loginClosure?()
    
    
    }
    
    @objc private func loginAction() {
        print("点击登陆")
        
        loginClosure?()
    }
    
}
